package controllers

import (
	study "demo/score_server"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"google.golang.org/protobuf/proto"
)

// ProtobufController operations for Protobuf
type ProtobufController struct {
	beego.Controller
}

// URLMapping ...
func (c *ProtobufController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Create
// @Description create Protobuf
// @Param	body		body 	models.Protobuf	true		"body for Protobuf content"
// @Success 201 {object} models.Protobuf
// @Failure 403 body is empty
// @router / [post]
func (c *ProtobufController) Post() {

}

// GetOne ...
// @Title GetOne
// @Description get Protobuf by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Protobuf
// @Failure 403 :id is empty
// @router /:id [get]
func (c *ProtobufController) GetOne() {
	// 初始化proto中的消息
	studyInfo := &study.StudyInfo{}

	//常规赋值
	studyInfo.Id = 1
	studyInfo.Name = "学习ProtoBuf"
	studyInfo.Duration = 180

	//在go中声明实例化map赋值给ProtoBuf消息中定义的map
	score := make(map[string]int32)
	score["实战"] = 100
	studyInfo.Score = score

	//用字符串的方式：打印ProtoBuf消息
	fmt.Printf("字符串输出结果：%v\n", studyInfo.String())
	fmt.Println()

	//转成二进制文件
	marshal, err := proto.Marshal(studyInfo)
	if err != nil {
		return
	}
	fmt.Printf("Marshal转成二进制结果：%v\n", marshal)
	fmt.Println()

	//将二进制文件转成结构体
	newStudyInfo := study.StudyInfo{}
	err = proto.Unmarshal(marshal, &newStudyInfo)
	if err != nil {
		return
	}
	fmt.Printf("二进制转成结构体的结果：%v\n", &newStudyInfo)
	fmt.Println()
}

// GetAll ...
// @Title GetAll
// @Description get Protobuf
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Protobuf
// @Failure 403
// @router / [get]
func (c *ProtobufController) GetAll() {

}

// Put ...
// @Title Put
// @Description update the Protobuf
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Protobuf	true		"body for Protobuf content"
// @Success 200 {object} models.Protobuf
// @Failure 403 :id is not int
// @router /:id [put]
func (c *ProtobufController) Put() {

}

// Delete ...
// @Title Delete
// @Description delete the Protobuf
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *ProtobufController) Delete() {

}
